/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package util.ejb;

import entities.tools.FilledForm;
import entities.users.FormSigner;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * Bean used for the management of the FormSigner entity. Contains crud
 * operations, search operations, and other useful tools
 *
 * @author Marek
 */
@Stateless
public class FormSignerBean implements FormSignerBeanInterface {

    @PersistenceContext(name = "test", unitName = "JEE-study-warPU")
    private EntityManager em;

    /**
     * Method creates new FormSigner instance
     *
     * @return new FormSigner instance
     */
    @Override
    public FormSigner createFormSigner() {
        FormSigner formSigner = new FormSigner();
        em.persist(formSigner);
        return formSigner;
    }

    /**
     * Method deletes specified by detachedFormSigner entity
     *
     * @param detachedFormSigner
     * @return true if operation succeeded, false - if not
     */
    @Override
    public boolean deleteFormSigner(FormSigner detachedFormSigner) {
        FormSigner attachedFormSigner = em.find(FormSigner.class, detachedFormSigner.getId());
        if (null == attachedFormSigner) {
            System.out.println("FormSignerBean:deleteFormSigner():FormSigner, you have specified, doesn't exist in the database!");
            return false;
        }
        Long size = (Long) em.createQuery("select count(filledForm) from FormSigner formSigner,FilledForm filledForm where formSigner=:formSigner and formSigner=filledForm.formSigner").setParameter("formSigner",attachedFormSigner).getSingleResult();
        if (size > 0) {
            System.out.println("FormSignerBean:deleteFormSigner():FormSigner cannot be deleted, becouse he had filled some forms before!");
            return false;
        }
        em.remove(attachedFormSigner);
        return true;
    }

    /**
     * Method updates in the database, specified by detachedFormSigner entity
     *
     * @param detachedFormSigner
     * @return true if operation succeeded, false - if not
     */
    @Override
    public boolean updateFormSigner(FormSigner detachedFormSigner) {
        FormSigner attachedFormSigner = em.find(FormSigner.class, detachedFormSigner.getId());
        if (null == attachedFormSigner) {
            System.out.println("FormSignerBean:updateFormSigner():FormSigner, you have specified, doesn't exist in the database!");
            return false;
        }
        em.merge(detachedFormSigner);
        return true;
    }

    /**
     * Method returns all FormSigner entities from the database
     *
     * @return all FormSigner from the database
     */
    @Override
    public List<FormSigner> getFormSigners() {
        return em.createQuery("select formSigner from FormSigner formSigner", FormSigner.class).getResultList();
    }

    /**
     * Method find in the detabase entity specified by id parameter, and returns
     * this entity
     *
     * @param id
     * @return founded FormSigner
     */
    @Override
    public FormSigner getFormSigner(Long id) {
        FormSigner attachedFormSigner = em.find(FormSigner.class, id);
        if (null == attachedFormSigner) {
            System.out.println("FormSignerBean:getFormSigner():FormSigner, you have specified, doesn't exist in the database!");
            return null;
        }
        return attachedFormSigner;
    }

    /**
     * Method find in the detabase entity specified by firstName, secondName,
     * phone, and email parameters, and returns it. Method returns null if no
     * users have been found.. this entity
     *
     * @param id
     * @return founded FormSigner
     */
    @Override
    public FormSigner getFormSigner(String firstName, String secondName, String phone, String email) {
        try {
            FormSigner formSigner = em.createQuery("select fs from FormSigner fs where fs.firstName=:firstName and fs.secondName=:secondName and fs.phone=:phone and fs.email=:email", FormSigner.class).setParameter("firstName", firstName).setParameter("secondName", secondName)
                    .setParameter("phone", phone)
                    .setParameter("email", email)
                    .getSingleResult();

            if (null == formSigner) {
                System.out.println("FormSignerBean:getFormSigner():FormSigner, you have specified, doesn't exist in the database!");
                return null;
            }
            return formSigner;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("FormSignerBean:getFormSigner():FormSigner, you have specified, doesn't exist in the database!");
            return null;
        }
    }

    /**
     * Method returns all forms filled by, entity, set in detachedFormSigner
     * param.
     *
     * @param detachedFormSigner
     * @return List of all filled forms
     */
    @Override
    public List<FilledForm> getFilledForms(FormSigner detachedFormSigner) {
        FormSigner attachedFormSigner = em.find(FormSigner.class, detachedFormSigner.getId());
        if (null == attachedFormSigner) {
            System.out.println("FormSignerBean:getFilledForms():FormSigner, you have specified, doesn't exist in the database!");
            return null;
        }
        return attachedFormSigner.getFilledForms();
    }

    /**
     * (Dangerous) Method is bypasses security policy. Normally FormSigner
     * cannot be deleted, when he had filled some forms before. Method is
     * performing delete operation on all instances of FilledForm entity related
     * to the Form Signer, specified by pram, and than it deletes also that
     * FormSigner entity, specified by param.
     *
     * @param detachedFormSigner
     * @return true - if operation succeeded false - otherwise
     */
    //TODO: Method need to be tested, if it works correctly
    @Override
    public boolean forcedDeleteFormSigner(FormSigner detachedFormSigner) {
        FormSigner attachedFormSigner = em.find(FormSigner.class, detachedFormSigner.getId());
        if (null == attachedFormSigner) {
            System.out.println("FormSignerBean:forcedDeleteFormSigner():The FormSigner, specified by you, doesn't exist in the database");
            return false;
        }
        List<FilledForm> filledForms = em.createQuery("select filledForm from FormSigner formSigner,FilledForm filledForm where formSigner=filledForm.formSigner and formSigner.id=:id", FilledForm.class).setParameter("id", attachedFormSigner.getId()).getResultList();
        for (FilledForm filledForm : filledForms) {
            em.remove(filledForm);
        }
        em.remove(attachedFormSigner);
        return true;
    }
}
